/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.cnt;

import cn.ac.iie.di.gwxs.*;
import cn.ac.iie.di.datadock.rdata.exchange.client.v1.session.RESendSession;
import cn.ac.iie.di.global.GlobalParas;
import cn.ac.iie.di.tools.MMUrlPath;
import cn.ac.iie.di.cnt.JsonCntSender.SessionPool;
import cn.ac.iie.di.tools.GenerateMD5;
import cn.ac.iie.di.tools.TimeFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import redis.clients.jedis.Jedis;

/**
 *
 * @author lenovo-pc
 */
public class JsonCntTask implements Runnable {

    private final List<HashMap<Long, Integer>> jsonList;

    static private final HashMap<String, String> JsonData;//存储Json中的数据
    static org.apache.log4j.Logger logger = null;

    static {
        logger = org.apache.log4j.Logger.getLogger(JsonCntTask.class.getName());
    }

    private SimpleDateFormat formatter;

    static {

        JsonData = new HashMap<>();

    }
    private final SessionPool pool;

    public JsonCntTask(List<HashMap<Long, Integer>> jsonList, SessionPool pool) {
        this.jsonList = jsonList;
        this.pool = pool;
        this.formatter = new SimpleDateFormat("yyyy-MM");
    }

    @Override
    public void run() {
        RESendSession session = null;
        long line = 0;
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH");
//        Jedis jedis = null;
        try {
            session = pool.borrowSession();
//            jedis = GlobalParas.pool.borrowSession();
            logger.debug("asdfzcvetqtert " + jsonList.size());
            for (HashMap<Long, Integer> json : jsonList) {
                if (json.size() == 0) {
                    continue;
                }
                try {
                    Set<Long> keys = json.keySet();
                    Iterator<Long> ite = keys.iterator();
                    while (ite.hasNext()) {
                        Long key = ite.next();
                        Integer value = json.get(key);
                        session.setString("key", String.valueOf(key));
                        session.setLong("value", value);
//                        jedis.incrBy(key, value);
                        try {
                            session.add();
                            line++;
                        } catch (Exception e) {
                            sendMSG(session);
                            logger.info("CNT successful send " + line);
                            line = 0;
                            session.add();
                            line++;
                        }
                    }

                } catch (Exception ex) {
                    logger.warn("The session add is wrong:" + ex + "error msg" + json, ex);
                }

            }
            sendMSG(session);
            logger.info("CNT successful send " + line);
            line = 0;

        } catch (Exception ex) {
            logger.error("know CNT Task error ", ex);
            ex.printStackTrace();
        } finally {
            if (session != null) {
                pool.returnSession(session);
            }
//            if (jedis != null) {
//                GlobalParas.pool.returnSession(jedis);
//            }
        }

    }

    public void sendMSG(RESendSession session) {
        while (true) {
            try {
                session.flush();
                break;
            } catch (Exception ex) {
                logger.info("what's up buddy ? how's it going ?" + ex, ex);
            }
        }
    }
}
